(function (window) {
    var pager = new pagerClass();

    window.jpager = pager;

})(window);

function pagerClass() {
    this.index = 1;
    this.pages = 1;
    this.size = 1;
    this.rows = 0;
    this.paginationId;

    this.init = function (pageIndex, pageCount, pageSize, pagerows, pagerId) {
        this.index = pageIndex;
        this.pages = pageCount;
        this.size = pageSize;
        this.rows = pagerows;
        this.paginationId = pagerId;
    };

    this.setRows = function (rows) {
        this.rows = rows;
        this.pages = Math.ceil(this.rows / this.size);
    };

    this.clear = function () {
        var pageHolder = this.paginationId == null ? jQuery(".pagination") : jQuery("#" + this.paginationId);
        pageHolder.html("");
    };

    this.process = function (callFunction, placeId) {
        var pageHolder = placeId == null ? jQuery(".pagination") : jQuery("#" + placeId);

        if (this.pages <= 1) { pageHolder.html(""); return; }

        //设置如果当前分页小于1,则设为第一页
        if (this.index < 1) { this.index = 1; }

        //设置如果当前分页大于总分页数,则当前分页为总分页数
        if (this.index > this.pages && this.pages > 0) {
            this.index = this.pages;
        }

        var str = "";

        //显示第一页
        if (this.index != 1) {
            str += "<a onclick=\"" + callFunction + "(" + eval(this.index - 1) + ")\"" + " href=\"javascript:;\" class='prev'>上一页</a> ";
        }

        k = 0; 
        //分页样式中间显示的条数索引
        y = 0; 
        //是否已显示前端符号

        for (var i = 1; i <= this.pages; i++) {
            if (i == 1 && i != this.index) {
                str += "<a onclick=\"" + callFunction + "(1)\" href=\"javascript:;\">" + i + "</a> ";
            }
            else {
                if (k < 5) {
                    if (eval(this.index - i) > 2 && eval(i + 4) < this.pages) {
                        if (y == 0) { str += ".."; y++; }
                        str += "";
                    }
                    else {
                        if (i == this.index) {
                            str += "<a class='on'>" + i + "</a> ";
                        }
                        else {
                            str += "<a onclick=\"" + callFunction + "(" + i + ")\" href=\"javascript:;\">" + i + "</a> ";
                        }
                        k++;
                    }
                }
                else {
                    str += "..<a onclick=\"" + callFunction + "(" + this.pages + ")\" href=\"javascript:;\">" + this.pages + "</a> ";
                    break;
                }
            }
        }

        var inputId = "index" + (placeId == null ? "" : placeId);
        if (this.index != this.pages) {
            str += "<a onclick=\"" + callFunction + "(" + (parseInt(this.index) + 1) + ")\" href=\"javascript:;\" class='next'>下一页</a>";
        }
        str += " 到第 <input type=\"text\" class=\"t\" size=\"3\" name=\"index\" onkeyup=\"verifyInputIndex(" + this.pages + ",'" + inputId + "')\" id=\"" + inputId + "\"  value=\"" + this.index + "\" /> 页 <button type=\"submit\" onclick=\"if($('#" + inputId + "').val() != '') " + callFunction + "($('#" + inputId + "').val());\">确定</button>";

        pageHolder.html(str);
    };
}

function verifyInputIndex(pages, controlId) {
    if (isNaN($('#' + controlId).val())) {
        $('#' + controlId).val("1");
    }
    else if (parseInt($('#' + controlId).val()) < 1) {
        $('#' + controlId).val("1");
    }
    else if (parseInt($('#' + controlId).val()) > pages) {
        $('#' + controlId).val(pages);
    }
}